﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Model;
using EntityServices;
using System.Web.Http.Cors;

namespace WebApp1.Controllers
{
    [EnableCors(origins: "*", headers: "*", methods: "*")]
    public class 学院Controller : ApiController
    {
        TimeTableOneEntityServicesFactory entityServices = new TimeTableOneEntityServicesFactory();
        // GET api/<controller>
        [HttpGet]
        [Route("api/学院/{Id}/系部")]

        public ICollection<系部> GetClassByCollege(int id)
        {
            return entityServices.学院Service.GetById(id).系部;
        }

        public 学院Controller()
            : base()
        {

        }



        public IEnumerable<学院> Get()
        {
            return entityServices.学院Service.GetList();
        }

        // GET api/<controller>/5

        [Route("api/学院/{Id}")]
        public 学院 Get(int id)
        {
            return entityServices.学院Service.GetById(id);
        }

        // POST api/<controller>
        public void Post([FromBody]学院 value)
        {
            entityServices.学院Service.NewSave(value);
        }
        [Route("api/学院/{Id}")]
        // PUT api/<controller>/5
        public void Put(int id, [FromBody]学院 value)
        {
            if (this.Get(id) != null && value.系部.Count == 0)
            {
                Delete(id);
                entityServices.学院Service.NewSave(value);

            }

        }

        [HttpDelete]
        [Route("api/学院/{Id}")]
        // DELETE api/<controller>/5
        public void Delete(int id)
        {
            学院 temp = this.Get(id);
            if (temp != null && temp.系部.Count==0)
            {
                
                entityServices.学院Service.Remove(temp);

            }


        }
    }
}